#include "localusersql.h"
#include "../common/singleton.h"
#include <QSqlQuery>


LocalUserSql::LocalUserSql()
{
}

bool LocalUserSql::insertUser(const QString &username, const QString &password)
{
    // 1. 打开数据库
    if(SingleTon<LocalUserDb>::getReference().open() == false) {return false;}

    // 2. 语句
    QSqlQuery query;
    query.prepare("insert into client(username, password)"
                  "values(:1, :2);");
    query.bindValue(":1", username);
    query.bindValue(":2", password);

    if(query.exec() == false){
        qDebug() << query.lastError().text();
        return false;
    }else{}

    SingleTon<LocalUserDb>::getReference().close();
    return true;
}

QStringList LocalUserSql::selectUser()
{

    return SingleTon<LocalUserDb>::getReference().selectUser();
}

QString LocalUserSql::selectPass(const QString& username)
{
        // 1. 打开数据库
    if(SingleTon<LocalUserDb>::getReference().open() == false) {return {false};}

    // 2. 语句
    QSqlQuery query;
    query.prepare("select password from client where username = :1;");
    query.bindValue(":1", username);

    if(query.exec() == false){
        qDebug() << query.lastError().text();
        return {};
    }else{
        if(query.next()){
            return query.value(0).toString();
        }
    }
    SingleTon<LocalUserDb>::getReference().close();
    return {};
}
